package com.second.first;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Info1kActivity extends Activity {
	/** Called when the activity is first created. */

	private MqttClient client;
	private MqttConnectOptions conOpt;
	final static String broker = "tcp://test.mosquitto.org:1883";

	// private EditText pubTxt;
	private Button pubBtn;
	private Button subBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		pubBtn = (Button) findViewById(R.id.pulishBtn);
		subBtn = (Button) findViewById(R.id.subBtn);
		
		try {
			// Construct the object that contains options used when connecting
			// the broker
			// such as CleanSession and password
			conOpt = new MqttConnectOptions();
			conOpt.setCleanSession(false);

			// Construct the MqttClient instance
			// NOTICE: The persistence mechanism is used to enable reliable
			// messaging.
			// For qualities of server (QoS) 1 or 2 to work, messages must be
			// persisted
			// to disk by both the client and the server. If this is not done,
			// then a failure
			// in the client or server will result in lost messages.
			// If the application is only sending QoS 0 messages, then this is
			// not needed.
			client = new MqttClient(broker, MqttClient.generateClientId(), null);

		} catch (MqttException e) {
			e.printStackTrace();
			Log.d("MQTT", "Unable to set up client: " + e.toString());
			System.exit(1);
		}

		pubBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Perform action on click
				EditText pubTxt = (EditText) findViewById(R.id.inputTxt);
				byte[] msg = pubTxt.getText().toString().getBytes();
				try {
					publish("cjtest/", 0, msg);
				} catch (MqttException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		subBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Perform action on click
				Intent i = new Intent();
				i.setClassName("com.second.first", "com.second.first.ListMsgActivity");
				startActivity(i);
			}
		});

	}
	

	/**
	 * Performs a single publish
	 * 
	 * @param topicName
	 *            the topic to publish to
	 * @param qos
	 *            the qos to publish at
	 * @param payload
	 *            the payload of the message to publish
	 * @throws MqttException
	 */
	public void publish(String topicName, int qos, byte[] payload)
			throws MqttException {

		// Connect to the server
		client.connect();

		// Construct an topic instance
		MqttTopic topic = client.getTopic(topicName);

		// Construct an instance of MQTT message
		MqttMessage message = new MqttMessage(payload);
		message.setQos(qos);

		// Publish the message

		MqttDeliveryToken token = topic.publish(message);

		// Wait until the message has been delivered to the server
		token.waitForCompletion();

		// Disconnect the client
		client.disconnect();

	}

}